<?php
/**
 * JobAsync
 *
 * @author PhuongTN
 */
class JobAsync
{
    /**
     * Job sync cache when add 1 article
     * @param array $params array('articleID') 
     * @author PhuongTN
     */
    public function addArticle($params)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            //init params
            $articleID = intval($params['ArticleID']);
            
            if($articleID > 0)
            {
                //init news instance
                $modelNews = News::getInstance();
            
                //add article to cache
                $response = $modelNews->addArticle($articleID);
            }
            else 
            {
                $response = Fpt_Server::setOutputData(-1, 'missing params', $params);
            }
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            $response = Fpt_Server::setOutputData(-1, '', '');
        }
        return $response;
    }
    
    /**
     * job edit article
     * @param type $data
     * @return type 
     */
    public function editArticle($params)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            $articleID = intval($params['ArticleID']);
            $modelNews = News::getInstance();
            $response = $modelNews->deleteArticle($articleID, array(), true);
            $response = $modelNews->addArticle($articleID);
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $response;
    }
    
    /**
     * job delete article
     * @param type $data
     * @return type 
     */
    public function deleteArticle($params)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            $articleID      = intval($params['ArticleID']);
            $articleDetail  = $params['ArticleDetail'];
            $modelNews  = News::getInstance();
            $response   = $modelNews->deleteArticle($articleID, $articleDetail);
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $response;
    }
    
    /**
     * set hot 1 articleid
     * @param array $params array(ArticleID);
     * @return type 
     */
    public function setHotArticle($params)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            //init params
            $articleIDs = $params['articleIDs'];
            if(is_array($articleIDs) && !empty($articleIDs))
            {
                //init news instance
                $modelNews = News::getInstance();
                
                foreach($articleIDs as $articleID)
                {
                    $articleID = intval($articleID);
                    //add article to cache
                    $response = $modelNews->setHotArticle($articleID);
                }
            }
            else 
            {
                $response = Fpt_Server::setOutputData(-1, 'missing params', $params);
            }
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            $response = Fpt_Server::setOutputData(-1, '', '');
        }
        return $response;
    }
    
    /**
     * unset hot article
     * @param type $params
     * @return type 
     */
    public function unsetHotArticle($params)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            //init params
            $articleID  = intval($params['ArticleID']);
            $cateID     = intval($params['CateID']);
            if($articleID > 0)
            {
                //init news instance
                $modelNews = News::getInstance();
                //add article to cache
                $response = $modelNews->unsetHotArticle($articleID, $cateID);
            }
            else 
            {
                $response = Fpt_Server::setOutputData(-1, 'missing params', $params);
            }
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            $response = Fpt_Server::setOutputData(-1, '', '');
        }
        return $response;
    }
    
    /**
     * update hot position all by cate
     * @param array $params array(CateID)
     * @return type 
     */
    public function updateHotByCate($params)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            //init params
            $cateID     = intval($params['cateID']);
            if($cateID > 0)
            {
                //init news instance
                $modelNews = News::getInstance();
                //add article to cache
                $response = $modelNews->updateHotByCate($cateID);
            }
            else 
            {
                $response = Fpt_Server::setOutputData(-1, 'missing params', $params);
            }
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            $response = Fpt_Server::setOutputData(-1, '', '');
        }
        return $response;
    }
    

    /**
     * job edit/delete article
     * @param type $data
     * @return type 
     */
    public function editCategory($params)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            $cateID = intval($params['CateID']);
            $modelCate = Category::getInstance();
            $response = $modelCate->editCategory($cateID);
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $response;
    }
    
    /**
     * job delete article
     * @param type $data
     * @return type 
     */
    public function editTag($params)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            $tagID = intval($params['TagID']);
            $modelNews = News::getInstance();
            $response = $modelNews->editTag($tagID);
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $response;
    }
     
    /**
     * job delete article
     * @param type $data
     * @return type 
     */
    public function editTeam($params)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            $teamID = intval($params['TeamID']);
            $modelTeams = TeamSeason::getInstance();
            $response = $modelTeams->editTeam($teamID);
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $response;
    }
    /**
     * 
     * @param type $params
     * @return type 
     */
    public function deleteArticleByCateID($params)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            $cateID = intval($params['CateID']);
            $modelNews  = News::getInstance();
            $response   = $modelNews->deleteArticleByCateID($cateID);
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $response;
    }
    
    /**
     * delete news by team
     * @param int $params
     * @return bool 
     * @author PhuongTN
     */
    public function deleteArticleByTeamID($params)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            $teamID = intval($params['TeamID']);
            $modelNews = News::getInstance();
            $response = $modelNews->deleteArticleByTeamID($teamID);
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $response;
    }
     
    /**
     * job delete article
     * @param type $data
     * @return type 
     */
    public function editTopic($params)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            $topicID = intval($params['TopicID']);
            $modelNews = News::getInstance();
            $response = $modelNews->editTopic($topicID);
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $response;
    }
    
    /**
     * delete news by topic
     * @param type $params
     * @return bool 
     * @author PhuongTN
     */
    public function deleteArticleByTopicID($params)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            $topicID = intval($params['TopicID']);
            $modelNews = News::getInstance();
            $response = $modelNews->deleteArticleByTopicID($topicID);
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $response;
    }
    
    /**
     * add articles to topicid
     * @param array $params (topic_id, article_ids)
     * @return type 
     */
    public function addTopicArticles($params)
    {
        $response = false;
        try
        {
            $topicID        = intval($params['topic_id']);
            $articleIDs     = $params['article_ids'];
            if(empty($topicID) || empty($articleIDs))
            {
                $response = Fpt_Server::setOutputData(-1, 'missing params', $params);
            }
            $modelNews = News::getInstance();
            $response = $modelNews->addTopicArticles($topicID, $articleIDs);
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $response;
    }
    
    /**
     * remove 1 article out of topicid
     * @param array $params (topic_id, article_ids)
     * @return type 
     */
    public function deleteTopicArticles($params)
    {
        $response = false;
        try
        {
            $topicID       = intval($params['topic_id']);
            $articleIDs     = $params['article_ids'];
            if(empty($topicID) || empty($articleIDs))
            {
                $response = Fpt_Server::setOutputData(-1, 'missing params', $params);
            }
            $modelNews = News::getInstance();
            $response = $modelNews->deleteTopicArticles($topicID, $articleIDs);
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $response;
    }
    
    /**
     * notify miss cache
     * @param type $params
     * @return bool 
     * @author PhuongTN
     */
    public function syncCache($params)
    {  
        
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
          
            //init params
            $className      = $params['className'];
            $functionName   = $params['funcName'];
            $objectType     = $params['objectType'];//news, cateofnew,topicofnew,tagofnew
            $args = $params['funcArgs'];
            $args[] = true;
            //init class instance
            $classInstance  = call_user_func(array($className, 'getInstance'));
            //call funcName
            if(is_array($args))
            {
                $detail = call_user_func_array(array($classInstance, $functionName), $args);
            }
            else
            {
                $detail = call_user_func(array($classInstance, $functionName));
            }
         
            if(!empty($detail))
            {
                //init news instance
                $modelNews = News::getInstance();
                if(isset($detail['data']))
                {
                    $data = $detail['data'];
                }
                elseif(is_array($detail))
                {
                    $data = $detail;
                }
                unset($detail);
               
                if($objectType == 'news')
                {
                    foreach($data as $articleID)
                    {
                        echo $articleID.', ';
                        //write all cache
                        $response = $modelNews->addArticle($articleID);
                    }
                }
            }
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
        return $response;
    }
    
    /**
     * Job sync cache for comment, view
     * @param type $params
     * @return type 
     */
    public function syncCacheStatistic($params)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            //init news instance
            $modelStatistic = Statictis::getInstance();

            //add article to cache
            $response = $modelStatistic->syncStatistic();
            
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            $response = Fpt_Server::setOutputData(-1, '', '');
        }
        return $response;
    }
    
    public function writeCache($params)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            
            $offset     = isset($params['offset'])?$params['offset']:$_SERVER['offset'];
            $rowMaxID   = isset($params['maxid'])?$params['maxid']:$_SERVER['maxid'];
            
            $modelNews = News::getInstance();
            echo $offset.'  '.$rowMaxID;
            for($i = $rowMaxID; $i >= $offset; $i--)
            {
                
                //add article to cache
                $response[] = $modelNews->addArticle($i, false);
                echo '
'.$i;
            }
            echo $i;
//            $modelDB = Fpt_Business_News_Adapter_MySql::getInstance();
//            $data = $modelDB->getListArticleByRangeID($offset, $maxID);
//            echo '  =========='.$maxID.','.$rowMaxID.":";
//            if(!empty($data))
//            {
//                
//                foreach($data as $articleID)
//                {
//                    
//                }
//            }
//            else
//            {
//                echo 'empty?';
//            }
//            
//            if($maxID < $rowMaxID)
//            {
//                $nextOffset = $maxID+$limit;
//                //push job
//                $jobParams = array();
//                $jobParams['class']     = 'JobAsync';
//                $jobParams['function']  = 'writeCache';
//                $jobParams['args']      = array(
//                    'offset'    => $nextOffset,
//                    'maxid'     => $rowMaxID
//                );
//
//                //Create job client
//                $jobClient = Fpt_Global::getJobClientInstance('sport');
//
//                //Register job
//                $response = $jobClient->doBackgroundTask(Fpt_Global::getJobFunction('sport', 'write_cache'), $jobParams);
//            }
            
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            $response = Fpt_Server::setOutputData(-1, '', '');
        }
        return $response;
    }
    
    
    public function deleteCache($params)
    {
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        try
        {
            
            $offset     = isset($params['offset'])?$params['offset']:$_SERVER['offset'];
            $rowMaxID   = isset($params['maxid'])?$params['maxid']:$_SERVER['maxid'];
            
            $modelNews = News::getInstance();
            echo $offset.'  '.$rowMaxID;
            for($i = $rowMaxID; $i >= $offset; $i--)
            {
                
                //add article to cache
                $response[] = $modelNews->deleteArticle($i, false);
                echo '
'.$i;
            }
            echo $i;
            
        }
        catch (Exception $ex)
        {
            //log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            $response = Fpt_Server::setOutputData(-1, '', '');
        }
        return $response;
    }
    
}
?>
